{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "732c6ac8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "webkb_cosine_lib-20%.txt_1:finished\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-323934571d61>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     36\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"{}_{}:finished\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfileName\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mfile_after_num\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     37\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m71\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 38\u001b[1;33m     \u001b[0msplit_data\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"webkb_cosine_lib-20%.txt\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.8\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfile_after_num\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-2-323934571d61>\u001b[0m in \u001b[0;36msplit_data\u001b[1;34m(fileName, test_sz, file_after_num)\u001b[0m\n\u001b[0;32m     31\u001b[0m             \u001b[0mftr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwritelines\u001b[0m\u001b[1;33m(\u001b[0m \u001b[0msamples\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     32\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 33\u001b[1;33m             \u001b[0mfte\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwritelines\u001b[0m\u001b[1;33m(\u001b[0m \u001b[0msamples\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     34\u001b[0m     \u001b[0mftr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     35\u001b[0m     \u001b[0mfte\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "def zly_getrandom(lis,nums):\n",
    "    import random\n",
    "    a=[]\n",
    "#     print(end_num)\n",
    "    while len(a)<nums:\n",
    "        t=random.choice(lis)\n",
    "        if t in a:pass\n",
    "        else:\n",
    "            a.append(t)\n",
    "    return a\n",
    "import os\n",
    "# 输入的分别是原始的数据 ，test的比例 ，第三个参数表示，train和test前面的序号，比如1train和1test\n",
    "def split_data(fileName, test_sz, file_after_num=1):\n",
    "    try: \n",
    "        os.mkdir('{}_不按照类别切分'.format(fileName))\n",
    "    except OSError as error: \n",
    "        pass\n",
    "    f = open(fileName, 'r', encoding='UTF-8')\n",
    "    samples = f.readlines()\n",
    "    f.close()\n",
    "    N = len(samples)\n",
    "    trainfile = \"{}_不按照类别切分/{}train.txt\".format(fileName,file_after_num)\n",
    "    testfile = \"{}_不按照类别切分/{}test.txt\".format(fileName,file_after_num)\n",
    "    ftr = open(trainfile, 'w', encoding='UTF-8')\n",
    "    fte = open(testfile, 'w', encoding='UTF-8')\n",
    "    # 写入文件\n",
    "    choose_lis = zly_getrandom(range(N),N*(1-test_sz))\n",
    "    for i in range(N):\n",
    "        lineArr = samples[i]\n",
    "        if i in choose_lis:\n",
    "            ftr.writelines( samples[i])\n",
    "        else:\n",
    "            fte.writelines( samples[i])\n",
    "    ftr.close()\n",
    "    fte.close()\n",
    "    print(\"{}_{}:finished\".format(fileName,file_after_num))\n",
    "for i in range(1,71):\n",
    "    split_data(\"webkb_cosine_lib-20%.txt\", 0.8, file_after_num=i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1d6c4db8",
   "metadata": {},
   "outputs": [],
   "source": [
    "fileName=\"webkb_cosine_lib-20%.txt\"\n",
    "try: \n",
    "    os.mkdir('{}_不按照类别切分'.format(fileName))\n",
    "except OSError as error: \n",
    "    pass\n",
    "f = open(fileName, 'r', encoding='UTF-8')\n",
    "samples = f.readlines()\n",
    "f.close()\n",
    "N = len(samples)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "26f1f1af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "webkb_cosine_lib-20%.txt_1:finished\n",
      "webkb_cosine_lib-20%.txt_2:finished\n",
      "webkb_cosine_lib-20%.txt_3:finished\n",
      "webkb_cosine_lib-20%.txt_4:finished\n",
      "webkb_cosine_lib-20%.txt_5:finished\n",
      "webkb_cosine_lib-20%.txt_6:finished\n",
      "webkb_cosine_lib-20%.txt_7:finished\n",
      "webkb_cosine_lib-20%.txt_8:finished\n",
      "webkb_cosine_lib-20%.txt_9:finished\n",
      "webkb_cosine_lib-20%.txt_10:finished\n",
      "webkb_cosine_lib-20%.txt_11:finished\n",
      "webkb_cosine_lib-20%.txt_12:finished\n",
      "webkb_cosine_lib-20%.txt_13:finished\n",
      "webkb_cosine_lib-20%.txt_14:finished\n",
      "webkb_cosine_lib-20%.txt_15:finished\n",
      "webkb_cosine_lib-20%.txt_16:finished\n",
      "webkb_cosine_lib-20%.txt_17:finished\n",
      "webkb_cosine_lib-20%.txt_18:finished\n",
      "webkb_cosine_lib-20%.txt_19:finished\n",
      "webkb_cosine_lib-20%.txt_20:finished\n",
      "webkb_cosine_lib-20%.txt_21:finished\n",
      "webkb_cosine_lib-20%.txt_22:finished\n",
      "webkb_cosine_lib-20%.txt_23:finished\n",
      "webkb_cosine_lib-20%.txt_24:finished\n",
      "webkb_cosine_lib-20%.txt_25:finished\n",
      "webkb_cosine_lib-20%.txt_26:finished\n",
      "webkb_cosine_lib-20%.txt_27:finished\n",
      "webkb_cosine_lib-20%.txt_28:finished\n",
      "webkb_cosine_lib-20%.txt_29:finished\n",
      "webkb_cosine_lib-20%.txt_30:finished\n",
      "webkb_cosine_lib-20%.txt_31:finished\n",
      "webkb_cosine_lib-20%.txt_32:finished\n",
      "webkb_cosine_lib-20%.txt_33:finished\n",
      "webkb_cosine_lib-20%.txt_34:finished\n",
      "webkb_cosine_lib-20%.txt_35:finished\n",
      "webkb_cosine_lib-20%.txt_36:finished\n",
      "webkb_cosine_lib-20%.txt_37:finished\n",
      "webkb_cosine_lib-20%.txt_38:finished\n",
      "webkb_cosine_lib-20%.txt_39:finished\n",
      "webkb_cosine_lib-20%.txt_40:finished\n",
      "webkb_cosine_lib-20%.txt_41:finished\n",
      "webkb_cosine_lib-20%.txt_42:finished\n",
      "webkb_cosine_lib-20%.txt_43:finished\n",
      "webkb_cosine_lib-20%.txt_44:finished\n",
      "webkb_cosine_lib-20%.txt_45:finished\n",
      "webkb_cosine_lib-20%.txt_46:finished\n",
      "webkb_cosine_lib-20%.txt_47:finished\n",
      "webkb_cosine_lib-20%.txt_48:finished\n",
      "webkb_cosine_lib-20%.txt_49:finished\n",
      "webkb_cosine_lib-20%.txt_50:finished\n",
      "webkb_cosine_lib-20%.txt_51:finished\n",
      "webkb_cosine_lib-20%.txt_52:finished\n",
      "webkb_cosine_lib-20%.txt_53:finished\n",
      "webkb_cosine_lib-20%.txt_54:finished\n",
      "webkb_cosine_lib-20%.txt_55:finished\n",
      "webkb_cosine_lib-20%.txt_56:finished\n",
      "webkb_cosine_lib-20%.txt_57:finished\n",
      "webkb_cosine_lib-20%.txt_58:finished\n",
      "webkb_cosine_lib-20%.txt_59:finished\n",
      "webkb_cosine_lib-20%.txt_60:finished\n",
      "webkb_cosine_lib-20%.txt_61:finished\n",
      "webkb_cosine_lib-20%.txt_62:finished\n",
      "webkb_cosine_lib-20%.txt_63:finished\n",
      "webkb_cosine_lib-20%.txt_64:finished\n",
      "webkb_cosine_lib-20%.txt_65:finished\n",
      "webkb_cosine_lib-20%.txt_66:finished\n",
      "webkb_cosine_lib-20%.txt_67:finished\n",
      "webkb_cosine_lib-20%.txt_68:finished\n",
      "webkb_cosine_lib-20%.txt_69:finished\n",
      "webkb_cosine_lib-20%.txt_70:finished\n"
     ]
    }
   ],
   "source": [
    "def split_data(fileName, samples, test_sz, file_after_num=1):\n",
    "    N = len(samples)\n",
    "    trainfile = \"{}_不按照类别切分/{}train.txt\".format(fileName,file_after_num)\n",
    "    testfile = \"{}_不按照类别切分/{}test.txt\".format(fileName,file_after_num)\n",
    "    ftr = open(trainfile, 'w', encoding='UTF-8')\n",
    "    fte = open(testfile, 'w', encoding='UTF-8')\n",
    "    # 写入文件\n",
    "    choose_lis = zly_getrandom(range(N),N*(1-test_sz))\n",
    "    for i in range(N):\n",
    "        lineArr = samples[i]\n",
    "        if i in choose_lis:\n",
    "            ftr.writelines( samples[i])\n",
    "        else:\n",
    "            fte.writelines( samples[i])\n",
    "    ftr.close()\n",
    "    fte.close()\n",
    "    print(\"{}_{}:finished\".format(fileName,file_after_num))\n",
    "for i in range(1,71):\n",
    "    split_data(\"webkb_cosine_lib-20%.txt\",samples, 0.8, file_after_num=i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3aca863",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py36",
   "language": "python",
   "name": "py36"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
